#描述了棋盘的相关环境

import copy
import Config
class Node():

    #初始化一个n*n的棋盘
    def __init__(self,move,value,env,playturn,deepth,isTerminal,selfPlay):

        #给出是哪一方走棋
        self.playturn = playturn

        self.env = copy.copy(env)

        self.children = []

        self.move  = move

        self.value = value

        #给定树的深度
        self.deep = deepth

        self.isTerminal = isTerminal

        #标明哪个玩家是本方
        self.selfPlay = selfPlay

    def checkTerminal(self):

        #如果孩子也是叶子节点
        if len(self.children) <= 0:
            self.isTerminal = True

            # if self.selfPlay == 1:
            #     self.value = self.env.final1Score - self.env.final2Score
            # else:
            #     self.value = self.env.final2Score - self.env.final1Score
            if self.selfPlay == 1:

                self.value = self.env.play1score - self.env.play2score + (self.env.final1Score - self.env.final2Score)
            else:
                self.value = self.env.play2score - self.env.play1score + (self.env.final2Score - self.env.final1Score)


        return self.isTerminal


    #给出子节点
    def getSuccessors(self,BoardSituation):

        deep = self.deep + 1

        #判断是非叶子节点和深度之内的点
        if deep <= Config.TREEDEEP and self.isTerminal == False:

            #给出下一步敌方的棋子
            if self.playturn == 1:
                opposite = self.env.play2state
                oppositeturn = 2

            if self.playturn == 2:
                opposite = self.env.play1state
                oppositeturn = 1

            allMoves = self.env.getAllLegalMove(opposite,oppositeturn)

            #去掉停止步骤，加快速度
            if "S" in allMoves:
                allMoves.remove("S")

            for move in allMoves:

                nowenv = copy.deepcopy(self.env)

                if self.playturn == 1:
                    childPlayturn = 2
                else:
                    childPlayturn = 1

                # 给出下一步的可能性
                next_state, value, is_terminal,_ = nowenv.playerstep(move, childPlayturn)

                #判断是不是有重复点，双人的标准是两个人在同一个格 得分相同
                boardKey = str(nowenv.play1state)  +  "_" \
                           + str(nowenv.play2state)  +  "_" \
                           + str(nowenv.play1score) + "_" \
                           + str(nowenv.play2score) + "_" \
                           + str(nowenv.final1Score) + "_" \
                           + str(nowenv.final2Score)

                if boardKey not in BoardSituation:

                    child = Node(value = value, env=nowenv, move=move,
                                 playturn=childPlayturn, deepth = deep,
                                 isTerminal= is_terminal, selfPlay= self.selfPlay)
                    self.children.append(child)
                    BoardSituation.add(boardKey)

        else:

            #发现是终止节点,首先置为终止节点
            self.isTerminal = True

            # if self.selfPlay == 1:
            #     self.value = self.env.final1Score - self.env.final2Score
            # else:
            #     self.value = self.env.final2Score - self.env.final1Score

            if self.selfPlay == 1:
                self.value = (self.env.play1score - self.env.play2score) + (self.env.final1Score - self.env.final2Score)
            else:
                self.value = (self.env.play2score - self.env.play1score) + (self.env.final2Score - self.env.final1Score)





    #给出子节点，只有本方
    def getOwnSuccessors(self):

        deep = self.deep + 1

        #判断是非叶子节点和深度之内的点
        if deep <= Config.TREEDEEP and self.isTerminal == False:

            #给出下一步我方的棋子
            if self.playturn == 1:
                state = self.env.play1state

            if self.playturn == 2:
                state = self.env.play2state

            allMoves = self.env.getAllLegalMove(state)

            #去掉停止步骤，加快速度
            if "S" in allMoves:
                allMoves.remove("S")

            for move in allMoves:

                nowenv = copy.deepcopy(self.env)


                # 给出下一步的可能性
                next_state, value, is_terminal,_ = nowenv.playerstep(move, self.playturn)

                child = Node(value = value, env=nowenv, move=move,
                             playturn=self.playturn, deepth = deep,
                             isTerminal= is_terminal, selfPlay= self.selfPlay)
                self.children.append(child)



        else:

            #发现是终止节点,首先置为终止节点
            self.isTerminal = True

            if self.selfPlay == 1:
                self.value = self.env.play1score
            else:
                self.value = self.env.play2score











